package com.wn.icloud.configuration.erp;

import org.json.JSONObject;

public class APIInvoke {

	public static String dbId = "5e7c7deb9a011b";
	public static String uid = "西电";
	public static String pwd = "000000";
	public static int lang = 2052;
	public static void main(String[] args) throws Exception {
		InvokeHelper.POST_K3CloudURL = "http://114.116.22.37/k3cloud/";
		InvokeHelper.Login(dbId, uid, pwd, lang);
		SaleOrderQuery("{\"FormId\":\"SAL_SaleOrder\",\"FieldKeys\":\"FBillNo,FSaleOrgId,FDocumentStatus,FCustId,FMaterialId,FMaterialName,FQty\",\"FilterString\":\"\",\"OrderString\":\"\",\"TopRowCount\":0,\"StartRow\":0,\"Limit\":0}");
	}
	public static void login(){
		try {
			InvokeHelper.Login(dbId, uid, pwd, lang);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//销售订单查询
//			SaleOrderQuery("");

			//生产订单查询
//			ManuOrderQuery("");

			//生产汇报单新增。（目前系统保存有问题）
//			MORPT();

			//齐套分析：齐套分析的逻辑是先新增生产备料单保存——进行齐套分析——读取缺料清单。（目前系统保存有问题）
//			MOAnalyse();
	}

	public static String SaleOrderQuery(String query) throws Exception {
		String a = "{\"FormId\":\"SAL_SaleOrder\",\"FieldKeys\":\"FBillNo,FSaleOrgId,FDocumentStatus,FCustId,FMaterialId,FMaterialName,FQty\",\"FilterString\":\"\",\"OrderString\":\"\",\"TopRowCount\":0,\"StartRow\":0,\"Limit\":0}";
		//销售订单查询。其中:FieldKeys为查询字段
		String sResult = InvokeHelper.Query(query);
		System.out.println(sResult);
		return sResult;

//		JSONArray jsobj = new JSONArray(sResult);
//
//		JSONArray orderList = jsobj.getJSONArray(0);
//		System.out.print("\nFBillNo:" + orderList.get(0).toString());
//		System.out.print("\nFSaleOrgId:" + orderList.get(1).toString());
//		System.out.print("\nFDocumentStatus:" + orderList.get(2).toString());
//		System.out.print("\nFCustId:" + orderList.get(3).toString());
//		System.out.print("\nFMaterialName:" + orderList.get(5).toString());
//		System.out.print("\nFQty:" + orderList.get(6).toString());
//		System.out.println("JSON--" + JSON.toJSONString(orderList));
//		//根据客户id获取客户编码
//		sResult = InvokeHelper.Query("{\"FormId\":\"BD_Customer\",\"FieldKeys\":\"FNumber\",\"FilterString\":\"FCUSTID="+orderList.get(3).toString()+"\"}");
//		jsobj = new JSONArray(sResult);
//		orderList = jsobj.getJSONArray(0);
//		System.out.print("\nFCustomerNumber:" + orderList.get(0).toString());
	}

	public static String ManuOrderQuery(String query) throws Exception {
		String a = "{\"FormId\":\"PRD_MO\",\"FieldKeys\":\"FBillNo,FSaleOrderId,FMaterialId,FMaterialName,FSpecification,FQty,FWorkShopID,FDocumentStatus,FPlanStartDate,FPlanFinishDate\",\"FilterString\":\"\",\"OrderString\":\"\",\"TopRowCount\":0,\"StartRow\":0,\"Limit\":0}";

		//销售订单查询。其中:FieldKeys为查询字段
		String sResult = InvokeHelper.Query(query);
		return sResult;
		/*JSONArray jsobj = new JSONArray(sResult);
		JSONArray orderList = jsobj.getJSONArray(0);
		System.out.print("\nFBillNo:" + orderList.get(0).toString());
		System.out.print("\nFSaleOrderId:" + orderList.get(1).toString());
		System.out.print("\nFMaterialId:" + orderList.get(2).toString());
		System.out.print("\nFMaterialName:" + orderList.get(3).toString());
		System.out.print("\nFSpecification:" + orderList.get(4).toString());
		System.out.print("\nFQty:" + orderList.get(5).toString());
		System.out.print("\nFWorkShopID:" + orderList.get(6).toString());
		System.out.print("\nFDocumentStatus:" + orderList.get(7).toString());
		System.out.print("\nFPlanStartDate:" + orderList.get(8).toString());
		System.out.print("\nFPlanFinishDate:" + orderList.get(9).toString());*/
	}

	private static void MORPT() throws Exception {

		//IsVerifyBaseDataField 检测基础资料合规。字段内容见说明
		String content = "{\"Creator\": \"西电\",\"IsDeleteEntry\": \"true\",\"IsVerifyBaseDataField\": \"false\",\"IsEntryBatchFill\": \"true\", \"ValidateFlag\": \"true\", \"NumberSearch\": \"true\", \"IsAutoSubmitAndAudit\": \"true\",\"Model\": { \"FBillType\": {\"FNUMBER\": \"SCHBD02_SYS\" },\"FDate\": \"2020-04-06\",\"FPrdOrgId\": {\"FNumber\": \"MO000003\" },\"FPrdOrgId\": { \"FNumber\": \"100\"},\"FWorkshipIdH\": {\"FNumber\": \"BM000003\"},\"FDescription\": \"测试汇报\", \"FEntity\": [{\"FEntryID\": 0,\"FSrcBillType\": \"PRD_MO\",\"FSrcBillNo\": \"MO000002\",\"FSrcInterId\":100004,\"FSrcEntrySeq\": \"1\",\"FMoId\": \"100004\",\"FMoEntryId\": \"100004\",\"FIsNew\": \"true\",\"FMaterialId\": {\"FNumber\": \"CH4444\"}, \"FReportType\": {\"FNumber\": \"HBLX01_SYS\"},\"FUnitID\": {\"FNumber\": \"Pcs\"},\"FWorkshipId\": {\"FNumber\": \"BM000003\"},\"FBomId\": {\"FNumber\": \"CH4444_V1.0\"},\"FQuaQty\":6,\"FFinishQty\":6 }]}}";
		String sResult = InvokeHelper.Save("PRD_MORPT", content);

		JSONObject jsobj = new JSONObject(sResult);
		JSONObject result = jsobj.getJSONObject("Result");
		JSONObject status = result.getJSONObject("ResponseStatus");

		if(status.get("IsSuccess").toString().equals("true")){
			System.out.print("\nInfos:success:");
		}else{
			System.out.print("\nErrors:" + status.getJSONArray("Errors").toString() );
		}

	}

	private static void MOAnalyse() throws Exception {

		//字段内容见说明
		String content = "{\"Parameters\": \"\",\"Numbers\": [\"PM000004\"],\"Ids\": \"\",\"Model\": {\"FBillNo\": \"PM000004\",\"FBillType\": {\"FNUMBER\": \"SCBL01_SYS\"},\"FDate\": \"2020-04-06\",\"FBusinessType\": \"1\",  \"FPrdOrgId\": {\"FNumber\": \"100\"},\"FWorkShopID0\": {\"FNumber\": \"BM000003\"},\"FEntity\": [{\"FEntryID\": 0,\"FMoBillNo\": \"MO000002\",\"FMoEntrySeq\": 0,\"FMaterialId\": {\"FNumber\": \"CH4444\"},\"FOrderBomId\": {\"FNUMBER\": \"CH4444_V1.0\"},\"FQty\": 10,\"FStockReadyQty\": 0,\"FPlanStartDate\": \"2020-04-06\",\"FPlanFinishDate\": \"2020-04-06\",}]}}";
		String sResult = InvokeHelper.ExecOperation("PRD_PREPAREMTRL", "Analyse",content);

		JSONObject jsobj = new JSONObject(sResult);
		JSONObject result = jsobj.getJSONObject("Result");
		JSONObject status = result.getJSONObject("ResponseStatus");

		if(status.get("IsSuccess").toString().equals("true")){
			System.out.print("\nInfos:success");
		}else{
			System.out.print("\nErrors:" + status.getJSONArray("Errors").toString() );
		}
	}
}